package mo.leetcode_top;

import java.util.Arrays;
import java.util.LinkedList;

public class L239 {

    public int[] maxSlidingWindow(int[] nums, int k) {

        int[] res = new int[nums.length-k+1];
        LinkedList<Integer> indexes = new LinkedList<>();

        for (int i = 0; i < nums.length; i++) {
            if (!indexes.isEmpty() && indexes.getFirst() == (i - k)) {
                indexes.removeFirst();
            }
            while (!indexes.isEmpty() && nums[indexes.getLast()] < nums[i]) {
                indexes.removeLast();
            }
            indexes.addLast(i);
            if (i >= k -1) {
                res[i-k+1] = nums[indexes.getFirst()];
            }
        }

        return res;
    }

    public static void main(String[] args) {
        L239 test = new L239();
        System.out.println(Arrays.toString(test.maxSlidingWindow(new int[]{1}, 1)));
    }

}
